package org.kevin.util;

/**
 * @ClassName: StringUtils
 * @Description: TODO
 * @Author: kevin
 * @Date: 2019/8/26 5:15 下午
 * @Version: 1.0
 */
public class StringUtils {

    public static String getStringValue(Object object) {
        if (object == null) {
            return "";
        }
        return object.toString();
    }

    public static int getStringLength(String str) {
        if (str == null) {
            return 0;
        }
        return str.getBytes().length;
    }

    public static boolean hasValue(Object object) {
        if (object == null || object.toString().trim().equals("") || object.toString().trim().equalsIgnoreCase("null")) {
            return false;
        }
        return true;
    }

    public static boolean isNumber(String str) {
        if (str == null || "".equals(str)) {
            return false;
        }
        String reg = "^[-|+]?\\d*([.]\\d+)?$";
        return str.matches(reg);
    }

    public static int toInteger(String str) {
        return Integer.parseInt(str);
    }

    /**
     * 功能：把数字转化成字符串, 并根据长度在前面补0
     * @param val
     * @param len
     * @return
     */
    public static String switchToString(int val, int len) {
        String strVal = "" + val;
        int strLen = strVal.length();
        if (strVal.length() < len) {
            for (int i = 0; i < len - strLen; ++i) {
                strVal = "0" + strVal;
            }
        }
        return strVal;
    }
}
